package com.hehao.kurunner.ui;


import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;

import com.hehao.kurunner.R;
import com.hehao.kurunner.common.BaseActivity;
import com.hehao.kurunner.common.BaseFragment;
import com.hehao.kurunner.ui.fragment.DynamicFragment;
import com.hehao.kurunner.ui.fragment.HomeFragment;
import com.hehao.kurunner.ui.fragment.MSGFragment;
import com.hehao.kurunner.ui.fragment.MineFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 主activity
 * @author HeHao
 * @time 2015/11/13 15:27
 * @email 139940512@qq.com
 */
public class MainActivity extends BaseActivity {

    private LinearLayout footer ;
    private FragmentManager fragmentManager ;
    private List<BaseFragment> fragments;

    /**记录当前处在第几页*/
    private int index;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_main);
        footer = getView(R.id.ll_footer);
        getViewWithClick(R.id.nav_home);
        getViewWithClick(R.id.nav_dynamic);
        getViewWithClick(R.id.nav_msg);
        getViewWithClick(R.id.nav_mine);
    }

    @Override
    protected void initData() {
        fragmentManager = getFragmentManager();
        if(fragments == null) {
            fragments = new ArrayList<>();
            fragments.add(new HomeFragment());
            fragments.add(new DynamicFragment());
            fragments.add(new MSGFragment());
            fragments.add(new MineFragment());
        }

        fragmentManager.beginTransaction().add(R.id.main_container, fragments.get(0)).commit();
        footer.getChildAt(0).setSelected(true);
        index = 0;
    }

    @Override
    protected void onViewClick(View v) {
        if(v.getId()==R.id.nav_home){
            changePager(0);
        }else if(v.getId() == R.id.nav_dynamic){
            changePager(1);
        }else if(v.getId() == R.id.nav_msg){
            changePager(2);
        }else if(v.getId() == R.id.nav_mine){
            changePager(3);
        }
    }

    /**
     * 改变页
     * @param position
     */
    private void changePager(int position){
        if(position == index)
            return ;
        if(footer.getChildCount() == 0)
            return ;
        footer.getChildAt(index).setSelected(false);
        footer.getChildAt(position).setSelected(true);
        Fragment from = fragments.get(index);
        Fragment to = fragments.get(position);
        index = position;
        switchFragment(from, to);
    }

    private void switchFragment(Fragment from, Fragment to) {
        if(from == null || to == null)
            return ;
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        //setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
        if(!to.isAdded()){
            transaction.hide(from).add(R.id.main_container, to).commit();
        }else {
            transaction.hide(from).show(to).commit();
            try{
                BaseFragment bf = (BaseFragment)to;
                if(bf != null) {
                    //TODO
                }
            }catch (Exception e){}
        }
    }
}
